#include "CAppCtx.h"
#include "QSettings.h"
#include <QCoreApplication>
#include <QDir>
#include <QDirIterator>
#include <qnamespace.h>

#pragma execution_character_set("utf-8")

CAppCtx *CAppCtx::self = nullptr;

CAppCtx::CAppCtx() { init(); }

void CAppCtx::init() {
  QDir tempDir;
  QSettings setting(QCoreApplication::applicationDirPath() + "/config.ini",
                    QSettings::IniFormat);
  if (setting.contains("ffmpeg/path")) {
    ffmpeg_path_ = setting.value("ffmpeg/path").toString();
  } else {
    ffmpeg_path_ = "C:/msys64/mingw64/bin/ffmpeg.exe";
  }

  if (setting.contains("model/path")) {
    model_path_ = setting.value("model/path").toString();
  } else {
    model_path_ = "C:/code/model";
  }

  if(setting.contains("device/ip")) {
    dev_ip_ = setting.value("device/ip").toString();
  } else {
    dev_ip_ = "192.168.1.100";
  }
}

int CAppCtx::SetFFMpegPath(QString path) {
  ffmpeg_path_ = path;
  QSettings setting(QCoreApplication::applicationDirPath() + "/config.ini",
                    QSettings::IniFormat);
  setting.setValue("ffmpeg/path", path);
  return 0;
}

int CAppCtx::SetModelPath(QString path) {
  model_path_ = path;
  QSettings setting(QCoreApplication::applicationDirPath() + "/config.ini",
                    QSettings::IniFormat);
  setting.setValue("model/path", path);
  return 0;
}
int CAppCtx::SetDevIP(QString ip) {
  dev_ip_ = ip;
  QSettings setting(QCoreApplication::applicationDirPath() + "/config.ini",
                    QSettings::IniFormat);
  setting.setValue("device/ip", ip);
  return 0;
}